/**
 * 
 */
package co.com.sed.controller;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import co.com.sed.model.datamanagers.DataManager;
import co.com.sed.model.util.Util;


public class AppServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
	private static final long serialVersionUID = 1L;
	private DataManager dataManager = null;
	
	
	public AppServlet() {
		super();
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		dataManager = new DataManager();
		dataManager.setDbURL(config.getInitParameter("urlDatabase"));
		dataManager.setDbUserName(config.getInitParameter("usuarioDatabase"));
		dataManager.setDbPassword(config.getInitParameter("claveDatabase"));

		ServletContext context = config.getServletContext();
		context.setAttribute("tituloAplicacion", config.getInitParameter("tituloAplicacion"));
		context.setAttribute("rutaBase", config.getInitParameter("rutaBase"));
		context.setAttribute("rutaImagenes", config.getInitParameter("rutaImagenes"));
		context.setAttribute("rutaEstilos", config.getInitParameter("rutaEstilos"));
		context.setAttribute("rutaScripts", config.getInitParameter("rutaScripts"));
		context.setAttribute("dataManager", dataManager);

		try { // carga el driver JDBC
			Class.forName(config.getInitParameter("driverJDBC"));
		} catch (ClassNotFoundException e) {
			Util.print(e.toString());
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(dataManager == null){
			dataManager = (DataManager) getServletContext().getAttribute("dataManager");
		}
	
		String base = "/jsp/cliente/";
		String url = base + "index.jsp";
		String action = request.getParameter("action");
		if (action != null) {
			if (action.equals("formMenuUserClient"))
				url = base + "formMenuUserClient.jsp";
			
			else if (action.equals("formMenuUserClient360")){
				url = base + "formMenuUserClient360.jsp";
			
			}else if (action.equals("formExamen360")){
				url = base + "formExamen360.jsp";
			
			}else if (action.equals("formMenuUserClientExC")){
				url = base + "formMenuUserClientExC.jsp";
			
			}else if (action.equals("formExamenExC")){
				url = base + "formExamenExC.jsp";
			
			}else if (action.equals("formExamen360"))
				url = base + "formExamen360.jsp";
			
			else if (action.matches("(showCart|(add|update|delete)Item)"))
				url = base + "ShoppingCart.jsp";
			else if (action.equals("checkOut"))
				url = base + "Checkout.jsp";
			else if (action.equals("orderConfirmation"))
				url = base + "OrderConfirmation.jsp";
		}
		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(url);
		requestDispatcher.forward(request, response);
	}
}